/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016/11/24 by CL $
*/
$(function(){

	//显示注册条款
	$(document).on("click", "[f='but_open_reg_clause']", function(){
		$("#reg_clause_box").show();
	});
	$(document).on("click", "[f='but_close_reg_clause']", function(){
		$("#reg_clause_box").hide();
	});
	
	//手机号码
	$(document).on("blur", "#mobile", function(){
		$_tips = $("#mobile_tips");
		$val_mobile = $(this).val();
		if (!isMobile($val_mobile)) {
			$_tips.html("请填写有效的手机号码");
			$_tips.show();
		}
		else {
			//检测是否可以注册
			$.ajax({
				type: "POST",
				url: _ROOT_PATH + "index.php?c=passport&a=check",
				cache: false,
				data: {check_type:"mobile", name:$val_mobile, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == "1") {
						$_tips.html("");
						$_tips.hide();
					}
					else {
						$error = "该手机号已被注册";
						if ($result.length > 0) {
							$error = $result;
						}
						$_tips.html($error);
						$_tips.show();
					}

				},
				error: function() {
					//ToastShow("系统繁忙，请稍候再试");
				}
			});
		}
	});

	//获取手机验证码
	$(document).on("click", "[f='send_mobilecode']", function(){
		$val_mobile = $("#mobile").val();
		$("#mobile_tips").html("");
		$("#mobile_tips").hide();
		if (!isMobile($val_mobile)) {
			$("#mobile_tips").html("请填写有效的手机号码");
			$("#mobile_tips").show();
			return false;
		}
		$(this).html("获取中...");
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport&a=send",
			cache: false,
			data: {send_type:"mobile", name:$val_mobile, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("发送成功，请查收手机短信！");
					_countDown("btn_send_mobilecode", "send_mobilecode");
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("发送失败，请检查手机号码是否可用");
					}
					
				}
				$("#btn_send_mobilecode").html("获取验证码");

			},
			error: function() {
				//ToastShow("系统繁忙，请稍后再试");
				$("#btn_send_mobilecode").html("获取验证码");
			}
		});

	});
	
	//检查手机验证码
	$(document).on("blur", "#mobilecode", function(){
		$_tips = $("#mobilecode_tips");
		$_tips.html("");
		$_tips.hide();

		if ($("#mobile").val().length > 0 && $(this).val().length > 0) {
			$.ajax({
				type: "POST",
				url: _ROOT_PATH + "index.php?c=passport&a=check",
				cache: false,
				data: {check_type:"mobilecode", name:$("#mobile").val(), checkcode:$(this).val(),  r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == "1") {
						$_tips.html("");
						$_tips.hide();
					}
					else {
						$error = "手机验证码错误";
						if ($result.length > 0) {
							$error = $result;
						}
						$_tips.html($error);
						$_tips.show();
					}

				},
				error: function() {
					//ToastShow("系统繁忙，请稍候再试");
				}
			});
		}
	});

	//选择性别
	$(document).on("click", "[f='sel_gender']", function(){
		$_id = $(this).attr("data-id");
		$("#gender").val($_id);
		$("[f='sel_gender']").removeClass("current");
		$(this).addClass("current");
	});

	//提交注册
	$(document).on("click", "[f='but_regpost']", function(){
		$obj_tips = $("#post_tips");
		$obj_tips.html("");

		//7.05.10
		$loading = $(this).attr("data-loading");
		if (typeof($loading) == "undefined") {
			$loading = "";
		}


		$mobile = $("#mobile").val();
		$mobilecode = $("#mobilecode").val();
		if (typeof($mobilecode) == "undefined") {
			$mobilecode = "--";
		}
		$password = $("#password").val();
		$ageyear = $("#ageyear").val();
		$agemonth = $("#agemonth").val();
		$ageday = $("#ageday").val();
		$dist1 = $("#dist1").val();
		$dist2 = $("#dist2").val();
		$dist3 = $("#dist3").val();
		$gender = $("#gender").val();
		
		
		if (!isMobile($mobile)) {
			$obj_tips.html("请填写有效的手机号码");
			//$.scrollTo("#mobile", 100);
			return false;
		}
		if ($mobilecode != "--") {
			if ($mobilecode.length == 0) {
				$obj_tips.html("请填写手机验证码");
				return false;
			}
		}
		if ($password.length < 6 || $password.length > 16) {
			$obj_tips.html("请填写6-16个字符的密码");
			return false;
		}
		if ($ageyear.length == 0 || $agemonth.length == 0 || $ageday.length == 0) {
			$obj_tips.html("请设置您的生日");
			return false;
		}
		if ($dist1.length == 0 || $dist2.length == 0) {
			$obj_tips.html("请选择您所在城市");
			return false;
		}
		if ($gender.length == 0) {
			$obj_tips.html("请选择您的性别");
			return false;
		}

		$sort = $("#sort").val();
		if (typeof($sort) == "undefined") {
			$sort = "";
		}
		else {
			if ($sort.length == 0) {
				$obj_tips.html("请选择注册目的");
				return false;
			}
		}

		$marry = $("#marry").val();
		if (typeof($marry) == "undefined") {
			$marry = "";
		}
		else {
			if ($marry.length == 0) {
				$obj_tips.html("请选择婚姻状况");
				return false;
			}
		}

		$height = $("#height").val();
		if (typeof($height) == "undefined") {
			$height = 0;
		}
		else {
			if ($height.length == 0) {
				$obj_tips.html("请选择身高");
				return false;
			}
		}

		$weight = $("#weight").val();
		if (typeof($weight) == "undefined") {
			$weight = 0;
		}
		else {
			if ($weight.length == 0) {
				$obj_tips.html("请选择体重");
				return false;
			}
		}
		$education = $("#education").val();
		if (typeof($education) == "undefined") {
			$education = 0;
		}
		else {
			if ($education.length == 0) {
				$obj_tips.html("请选择学历");
				return false;
			}
		}
		$salary = $("#salary").val();
		if (typeof($salary) == "undefined") {
			$salary = 0;
		}
		else {
			if ($salary.length == 0) {
				$obj_tips.html("请选择年收入");
				return false;
			}
		}

		$job = $("#job").val();
		if (typeof($job) == "undefined") {
			$job = 0;
		}
		else {
			if ($job.length == 0) {
				$obj_tips.html("请选择职业");
				return false;
			}
		}

		$house = $("#house").val();
		if (typeof($house) == "undefined") {
			$house = 0;
		}
		else {
			if ($house.length == 0) {
				$obj_tips.html("请选择购房情况");
				return false;
			}
		}
		$car = $("#car").val();
		if (typeof($car) == "undefined") {
			$car = 0;
		}
		else {
			if ($car.length == 0) {
				$obj_tips.html("请选择购车情况");
				return false;
			}
		}
		$checkcode = $("#checkcode").val();
		if (typeof($checkcode) == "undefined") {
			$checkcode = "";
		}
		else {
			if ($checkcode.length == 0) {
				$obj_tips.html("请填写图片验证码");
				return false;
			}
		}
		if ($loading.length > 0) {
			$("#"+$loading).show();
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport",
			cache: false,
			data: {
					a:"regpost", mobile:$mobile, mobilecode:$mobilecode, password:$password,
					ageyear:$ageyear, agemonth:$agemonth, ageday:$ageday, dist1:$dist1, dist2:$dist2, dist3:$dist3,
					gender:$gender, sort:$sort, marry:$marry, height:$height, weight:$weight, education:$education,
					salary:$salary, job:$job, car:$car, house:$house, checkcode:$checkcode, datatype:"ajax", r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					ToastShow("恭喜你，注册成功");
					setTimeout(function(){
						window.location.href = _ROOT_PATH+"index.php?c=passport&a=headimg";
					}, 600);
				}
				else if ($response == "2") { //整合OEM
					if ($result.length > 0) {
						window.top.location.href = $result;
					}
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("注册失败，请检查资料！");
					}
					if ($loading.length > 0) {
						$("#"+$loading).hide();
					}
					$("#img_checkcode").attr("src", _ROOT_PATH+"source/include/validcode.php?type=number&width=65&height=30&num=5&r="+get_rndnum(8));
				}

			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});


	});

	//刷新图片验证码
	$(document).on("click", "[f='but_refresh']", function(){
		$("#img_checkcode").attr("src", _ROOT_PATH+"source/include/validcode.php?type=number&width=65&height=30&num=5&r="+get_rndnum(8));
	});


});

//倒计时
var cd_timer = 60; //1分钟
function _countDown(obj_id, attr_f) {
	$obj = $("#"+obj_id);
	if (cd_timer == 0) {
		$obj.html("获取验证码");
		$obj.removeClass("current");
		$obj.attr("f", attr_f);
		cd_timer = 60; 
	}
	else {
		$obj.removeClass("current");
		$obj.addClass("current"); 
		$obj.html(cd_timer+"秒重新发送");
		$obj.attr("f", "");
		cd_timer--; 
		setTimeout(function() { 
			_countDown(obj_id, attr_f)
		},	1000);
	}
} 